import * as Router from 'koa-router'
import * as recruitService from '../services/recruit.service'
import * as Joi from 'joi'
import { badParams } from '../stats'
import { ObjectId } from 'mongodb'

const router = new Router({
  prefix: '/api/recruit'
})

// 首页推荐职位
router.post('/home/recruit', async ctx => {
  const schema = Joi.object({
    keyWords: Joi.string(),
    workYear: Joi.number(),
    education: Joi.number(),
    salary: Joi.string(),
    city: Joi.string(),
    companySize: Joi.number(),
    financeStage: Joi.number()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const result = await recruitService.find(value)
  if (value.keyWords !== undefined) {
    result.sort(function (a, b) {
      return (
        LCS(b.positionName, value.keyWords) -
        LCS(a.positionName, value.keyWords)
      )
    })
  }
  ctx.body = {
    stat: 'OK',
    result
  }
})

// 首页推荐职业
router.post('/home/keyword', async ctx => {
  const schema = Joi.object({
    keyWords: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const result = await recruitService.find(value)
  ctx.body = {
    stat: 'OK',
    result
  }
})

// 职位详细页： 根据职位id，返回职位详细信息
router.post('/detail', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const result = await recruitService.findOne(value.id)
  if (result) {
    ctx.body = {
      stat: 'OK',
      result
    }
  } else {
    ctx.body = {
      stat: 'ERR_NOT_LOGIN',
      message: '无法获取到职位信息'
    }
  }
})

// 根据职位中的公司id查找这个公司
router.post('/detail/company', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const rows = await recruitService.listRecruit(value.id)
  ctx.body = {
    stat: 'OK',
    rows
  }
})

// 获取发布职位列表  接收hr的id，查职位表发布人的id，返回职位信息
router.post('/admin/recruitList', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const rows = await recruitService.listRecs(value.id)
  ctx.body = {
    stat: 'OK',
    rows
  }
})

// 添加职位信息
router.post('/admin/add', async ctx => {
  const token = ctx.cookies.get('token')
  const schema = Joi.object({
    positionName: Joi.string().required(),
    salary: Joi.object().required(),
    city: Joi.string().required(),
    workYear: Joi.number().required(),
    education: Joi.number().required(),
    location: Joi.string().required(),
    keyWords: Joi.string().required(),
    describe: Joi.string().required()
  })
  const { value } = schema.validate(ctx.request.body)
  value.time = new Date().toLocaleDateString()
  value.updatedAt = value.time
  value.keyWords = value.keyWords.toString().split(',')
  const result = await recruitService.add(value, token)
  ctx.body = {
    stat: 'OK',
    _id: result
  }
})

// 编辑职位
router.post('/admin/edit', async ctx => {
  const schema = Joi.object({
    _id: Joi.string().required(),
    positionName: Joi.string(),
    salary: Joi.object(),
    city: Joi.array(),
    workYear: Joi.number(),
    education: Joi.number(),
    location: Joi.string(),
    keyWords: Joi.array(),
    describe: Joi.string()
  })
  const { value, error } = schema.validate(ctx.request.body)
  value._id = new ObjectId(value._id)
  value.updatedAt = new Date().toLocaleDateString()
  if (value.keyWords.constructor === String) {
    const str = value.keyWords.toString()
    value.keyWords = str.split(',')
  }
  await recruitService.update(value._id, value)
  ctx.body = {
    stat: 'OK'
  }
})

// 删除职位
router.post('/admin/remove', async ctx => {
  const schema = Joi.object({
    id: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const result = await recruitService.remove(value.id)
  ctx.body = {
    stat: 'OK',
    result
  }
})

// 公司详情页 热招职位
router.post('/hotRecruit', async ctx => {
  const schema = Joi.object({
    companyId: Joi.string().required()
  })
  const { value, error } = schema.validate(ctx.request.body)
  if (error) throw badParams(error.message)
  const result = await recruitService.companyRe(value.companyId)
  ctx.body = {
    stat: 'OK',
    result
  }
})

// 最长公共子串算法
function LCS(str1: string, str2: string) {
  let count = 0
  for (let i = 0; i < str1.length; i++) {
    for (let j = str1.length; j > i; j--) {
      const strc = str1.slice(i, j)
      if (str2.indexOf(strc) !== -1) {
        const len = strc.length
        if (len > count) {
          count = len
        }
        break
      }
    }
  }
  return count
}

export default router
